package core;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

public class ParticleSystems
{
	private static ArrayList<ParticleSystem> systems = new ArrayList<ParticleSystem>();
	private static ArrayList<Rectangle> obstacles = new ArrayList<Rectangle>();
	
	public static boolean addParticleSystem(ParticleSystem s)
	{
		if (!particleSystemExists(s.getName()))
		{
			systems.add(s);
			return true;
		}
		return false;
	}
	
	private static boolean particleSystemExists(String name)
	{
		if (systems == null)
			return false;
		for (ParticleSystem s : systems)
		{
			String n = s.getName().trim();
			if (n.equalsIgnoreCase(name.trim()))
				return true;
		}
		return false;
	}
	
	public static ParticleSystem getParticleSystem(String name)
	{
		if (systems == null)
			return null;
		for (ParticleSystem s : systems)
		{
			String n = s.getName().trim();
			if (n.equalsIgnoreCase(name.trim()))
				return s;
		}
		return null;
	}
	
	public static boolean removeMaterial(String name)
	{
		if (systems == null)
			return false;
		for (int i = 0; i < systems.size(); i++)
		{
			ParticleSystem s = systems.get(i);
			String n = s.getName().trim();
			if (n.equalsIgnoreCase(name.trim()))
			{
				systems.remove(i);
				return true;
			}
		}
		return false;
	}
	
	public static void addObstacle(int x, int y, int w, int h)
	{
		obstacles.add(new Rectangle(x, y, w, h));
	}
	
	public static int getParticlesCount()
	{
		int c = 0;
		for (ParticleSystem s : systems)
		{
			c += s.getParticleCount();
		}
		return c;
	}
	
	public static void update(int w, int h)
	{
		for (ParticleSystem s : systems)
		{
			s.update(w, h, obstacles);
		}
	}
	
	public static void render(Graphics g)
	{
		for (ParticleSystem s : systems)
		{
			s.render(g);
		}
	}
	
	public static void renderObstacles(Graphics g)
	{
		g.setColor(Color.orange);
		for (Rectangle r : obstacles)
		{
			g.fillRect(r.x, r.y, r.width, r.height);
		}
	}
}
